package com.hyb.hybmodulesblog.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.constant.ArticleConstant;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.sign.Base64;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hyb.hybmodulesblog.domain.Article;
import com.hyb.hybmodulesblog.service.IArticleService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 文章表增删改查Controller
 * 
 * @author hyb
 * @date 2023-03-06
 */
@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController
{
    @Autowired
    private IArticleService articleService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    @InnerAuth
    @Log(title = "文章表增删改查", businessType = BusinessType.UPDATE)
    @PostMapping("/inner/update")
    public R<Article> innerUpdate(@RequestBody Article article){
        articleService.updateArticle(article);
        return R.ok(article);
    }

    @InnerAuth
    @Log(title = "文章表增删改查", businessType = BusinessType.INSERT)
    @GetMapping("/inner/get")
    public R<Article> get(@RequestParam("id")Long id){
        Article article = articleService.selectArticleById(id);
        return R.ok(article);
    }

    @InnerAuth
    @Log(title = "文章表增删改查", businessType = BusinessType.INSERT)
    @PostMapping("/inner/getBody")
    public R<List<Article>> getBody(@RequestBody Article article){
        List<Article> articles = articleService.selectArticleList(article);
        return R.ok(articles);
    }

    @InnerAuth
    @Log(title = "文章表增删改查", businessType = BusinessType.INSERT)
    @PostMapping("/inner/add")
    public AjaxResult addInner(@RequestBody Article article)
    {
        try {
            String p=redisTemplate.opsForValue().get(ArticleConstant.DESC_KEY+SecurityUtils.getUserId());
            byte[] decode = Base64.decode(p);
            article.setArticleDes(decode==null?"":new String(decode));
            articleService.insertArticle(article);
            return AjaxResult.success().put("id",article.getId());
        }catch (Exception e){
            return AjaxResult.error();
        }

    }


    /**
     * 查询文章表增删改查列表
     */
    @RequiresPermissions("article:article:list")
    @GetMapping("/list")
    public TableDataInfo list(Article article)
    {
        startPage();
        List<Article> list = articleService.selectArticleList(article);
        return getDataTable(list);
    }

    /**
     * 导出文章表增删改查列表
     */
    @RequiresPermissions("article:article:export")
    @Log(title = "文章表增删改查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Article article)
    {
        List<Article> list = articleService.selectArticleList(article);
        ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
        util.exportExcel(response, list, "文章表增删改查数据");
    }

    /**
     * 获取文章表增删改查详细信息
     */
    @RequiresPermissions("article:article:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(articleService.selectArticleById(id));
    }

    /**
     * 新增文章表增删改查
     */
    @RequiresPermissions("article:article:add")
    @Log(title = "文章表增删改查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Article article)
    {
        return toAjax(articleService.insertArticle(article));
    }

    /**
     * 修改文章表增删改查
     */
    @RequiresPermissions("article:article:edit")
    @Log(title = "文章表增删改查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Article article)
    {
        return toAjax(articleService.updateArticle(article));
    }

    /**
     * 删除文章表增删改查
     */
    @RequiresPermissions("article:article:remove")
    @Log(title = "文章表增删改查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(articleService.deleteArticleByIds(ids));
    }
}
